
<template>
  <div class="sidebar" :style="{ 'top': topmenuCollapse ? '0' : '35px', 'bottom': '25px' }">
    <el-menu :collapse="collapse" :default-active="onRoutes" @select="handleSelect" class="sidebar-el-menu" unique-opened>
      <el-popover placement="right-start" trigger="click" v-for="(item, index) in items" :key="index"
        :visible-arrow="false" v-model="item.submenushow">
        <ul class="popul" v-if="item.shortcuts.length > 0">
          <li v-for="(sitem, sindex) in item.shortcuts" :key="sindex" class="text item"
            @click="quickitemclick(sitem, item, $event)"
            :style="sitem.headline === 0 ? '' : 'border-top:solid 1px #fff;'">
            {{ sitem.title }}

          </li>
        </ul>
        <div slot="reference">
          <el-menu-item :index="item.index" :key="item.index" style="padding:0 11px;">
            <!-- <template>
              {{ item.icon }}
            </template> -->
            <i :class="item.icon" :style="'color:' + item.iconcolor + ';'"></i>

            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
        </div>
      </el-popover>
    </el-menu>
    <div class="sidewarn" v-if="warnstr" v-html="warnstr"></div>
  </div>
</template>

<script>
import { log } from "util";
export default {
  data() {
    return {
      collapse: true,
      topmenuCollapse: false,
      items: [],
      currentIndex: "",
      warnstr: null,
      lis310multiple: "0",
    };
  },
  computed: {
    onRoutes() {
      return this.currentIndex;
    },
  },
  methods: {
    quickitemclick(p, iobj, e) {
      this.$tool.setSession("currentmenu", p.index + " " + p.title);
      iobj.submenushow = false;
      this.$bus.$emit("topmenuMouseout", true);
      let path = p.index;
      if (this.lis310multiple > 0) {
        if (path.indexOf("LIS310") > -1 || path.indexOf("LIS311") > -1 || path.indexOf("LIS312") > -1 || path.indexOf("LIS313") > -1 || path.indexOf("LIS314") > -1) {
          path = this.$pub.getlis310path(this, path);
        }
      }
      if (path.indexOf("http") > -1) {
        if (typeof jsObj != "undefined") {
          if (path.indexOf("[currentuserno]") > -1) {
            path = path.replace("[currentuserno]", this.$tool.getSession(this.$tool.loginuserno));
          }
          jsObj.ReadCard("runurl:" + path);
        } else {
          this.$alert("请使用" + this.$pub.clientname + "打开！", "提示");
        }
      } else {
        this.$router.push("/" + path);
      }
    },
    handleSelect(key, keyPath) {
      //左侧每个菜单点击也触发首页切换事件
      this.$bus.$emit("changeModel", key);
      this.currentIndex = key;
      var ClickItem = this.items.find((item) => {
        if (item.index === this.currentIndex) {
          return item;
        }
      });
      if (ClickItem.subs) this.$bus.$emit("menuClick", ClickItem.subs);
    },
    async loadMenu() {
      let bigmodel = this.$tool.getLocalConfig("bigmodel");
      if (!bigmodel) {
        bigmodel = "LIS";
      }

      // console.log("token", this.$tool.getSession(this.$tool.logintoken));
      const response = await this.$tool.request({
        url: "ucent/syspanel",
        method: "post",
        data: {
          m: bigmodel,
        },
      });
      this.items = response.menu;
      // console.log("this.items", this.items);
      this.$tool.setLocalConfig("bigmodel", response.m);
      this.$tool.setLocalConfig("bigmodellist", response.ms);

      sessionStorage.setItem("buttonpremissions", JSON.stringify(response.buttonpremissions));

      if (response.alertmsg && response.alertmsg.length > 0) {
        this.warnstr = response.alertmsg;
      }
      if (response.unauth == "1") {
        this.$tool.setSession("unauth", "1"); //未授权，控制打印报告
      }
      if (bigmodel == "BAC") {
        // 获取微生物阳性检测
        // console.log(bigmodel);
        this.queryPositivelist();
      } else if (bigmodel == "INV") {
        this.invItemInfoAlert();
      } else if (bigmodel == "DEV") {
        this.devHandExeAlert();
      } else if (bigmodel == "LIS") {
        this.reportAlert();
      }
      this.$bus.$emit("changeModel", bigmodel);
      if (this.items && this.items.length > 0) {
        this.$tool.removeSession("noreg");
        this.currentIndex = this.items[0].index;
        this.$bus.$emit("menuClick", this.items[0].subs);
      } else {
        this.$tool.setSession("noreg", "1");
      }
    },
    closequickmenu() {
      for (let i = 0; i < this.items.length; i++) {
        if (this.items[i].submenushow) this.items[i].submenushow = false;
      }
    },
    invItemInfoAlert() {
      this.$tool
        .request({
          url: "ucent/invalert",
          method: "post",
          data: {
            action: "invItemInfoAlert",
          },
        })
        .then((res) => {
          //  console.log("er", res);
          this.$bus.$emit("invAlert", res);
        })
        .catch((e) => {
          this.$alert(e, "出错");
        });
    },
    devHandExeAlert() {
      this.$tool
        .request({
          url: "ucent/devalert",
          method: "post",
          data: {
            action: "showdevplanlist",
          },
        })
        .then((res) => {
          this.$bus.$emit("DevAlert", res);
        })
        .catch((e) => {
          this.$alert(e, "出错");
        });
    },
    reportAlert() {
      this.$tool
        .request({
          url: "ucent/reportalert",
          method: "post",
          data: {
            action: "showreportlist",
          },
        })
        .then((res) => {
          this.$bus.$emit("ReportAlert", res);
        })
        .catch((e) => {
          this.$alert(e, "出错");
        });
    },
    queryPositivelist() {
      this.$tool.request({
        url: "ucent/bactalert",
        method: "post",
        data: {
          action: "positivelist",
        },
      })
        .then((response) => {
          if (response.flag == "1") {
            if (response.data && response.data.length > 0) {
              this.$bus.$emit("bactAlert", response.data);
            }
          } else {
            this.$pub.message(this, response.msg, "error");
          }
        })
        .catch((erro) => { });
    },
  },
  created() {
    this.topmenuCollapse = this.$tool.getLocalConfig("topmenuCollapse") == "1";
    this.loadMenu();
    this.lis310multiple = this.$tool.getLocalConfig("lis310multiple");
   
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    this.$bus.$on("collapse", (msg) => {
      // console.log("msg", msg);
      this.collapse = msg;
      this.closequickmenu();
    });
    this.$bus.$on("topmenuCollapse", (msg) => {
      this.topmenuCollapse = msg;
      this.closequickmenu();
    });
    this.$bus.$on("switchmodel", (msg) => {
      this.loadMenu();
    });
  },
  beforeDestroy() {
    this.$bus.$off("collapse");
    this.$bus.$off("switchmodel");
    this.$bus.$off("topmenuCollapse");
  },
};
</script>

<style scoped>
.popul {
  background-color: #2d363b;
  color: #ffffff;
  padding: 5px 0;
}

.popul li {
  list-style: none;
  height: 30px;
  line-height: 30px;
  padding: 0 12px;
  cursor: pointer;
}

.popul li:hover {
  background-color: #467259;
}

.sidewarn {
  position: absolute;
  bottom: 0px;
  background: red;
  color: white;
  padding: 5px;
  border-radius: 5px;
  margin: 9px;
  text-align: center;
}

.sidebar {
  display: block;
  position: absolute;
  left: 0;
  bottom: 0;
  overflow-y: auto;
}

.sidebar::-webkit-scrollbar {
  width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
  width: 140px;
}

.sidebar>ul {
  height: 100%;
}

::v-deep li.el-menu-item {
  border-left: none;
  color: #ffffff;
  font-size: 14px;
  /* font-weight: 600; */
}

::v-deep li.el-menu-item i {
  font-size: 28px;
  color: #ffffff;
  font-weight: 590;
}

::v-deep .el-menu--collapse {
  width: 50px;
}

::v-deep .el-tooltip {
  padding: 0 11px !important;
}

::v-deep .el-menu {
  border-right: solid 1px #ccc;
  background-color: #2d363b !important;
}

::v-deep li.el-menu-item.is-active {
  background-color: #20c465 !important;
  color: #ffffff !important;
}

::v-deep li.el-menu-item:not(.is-active):hover {
  background-color: #467259 !important;
  color: #ffffff !important;
}

/* ::v-deep .el-menu-item {
} */
</style>
